function OrderController(free_days) {
	
    var oc = this;
    this.free_days = free_days;
        
	this.init = function() {
        
        $(function() {
            $( "#order_from_date_picker" ).datepicker({
                changeMonth: true,
                dateFormat: "yy-mm-dd",
                beforeShowDay: oc.disableDates,
                onSelect: function(dateText, inst) {
                    $('#order_from_order_form').val(dateText);
                }
            });
            $( "#order_to_date_picker" ).datepicker({
                defaultDate: "+1m",
                changeMonth: true,
                dateFormat: "yy-mm-dd",
                beforeShowDay: oc.disableDates,
                onSelect: function(dateText, inst) {
                    $('#order_to_order_form').val(dateText);
                }
            });
        });
        
        $('#send_order_form').click(function() {
                oc.sendOrder(this);
        });
    },
    
    this.disableDates = function(dt) {
        
        var m = dt.getMonth(); 
        var d = dt.getDate();
        var y = dt.getFullYear();
        if (new Date() > dt) {
            return [false];
        }
        
        for (var key in oc.free_days) {
            //alert(oc.free_days[key] +" = "+ ((m+1) + '-' + d + '-' + y))
            if (oc.free_days[key] == ((m+1) + '-' + d + '-' + y)) {
                return [false];
            }
        }
        return [true];
    },
	
	/*
	 *
	 */
	this.sendOrder = function() {
	    
        var vote = $("#vote_order_form").val();
        var rating = "";
        if (vote == "false") {
            rating = $("#rating_order_form").val();
        }
        var json_data = JSON.stringify({
            car: $("#order_from_car").val(), 
            order_from: $("#order_from_order_form").val(), 
            order_to: $("#order_to_order_form").val(), 
            finish_place: $("#finish_place_order_form").val(),
            rating: rating
        });
        $.ajax({
            type: "POST",
            url: "order_add.htm",
            data: {
                data: json_data
            },
            dataType: "json",
            success: function(response) {
                if (response.res == "false") {
                    alert(response.error);
                }
                window.location.href = "order_view.htm?id="+response.item;
            }
        });
	}
	
}


